package com.hsmw.api.controller.biz;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsmw.api.service.HsmwRoleService;
import com.hsmw.api.service.HsmwUserService;
import com.hsmw.common.base.BaseController;
import com.hsmw.common.base.BasePage;
import com.htn.common.core.base.DataResponse;
import com.hsmw.domain.HsmwRole;
import com.hsmw.domain.HsmwUser;
import com.hsmw.domain.form.GetObjectByIdForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author WD
 */
@Api(tags = "BIZ-用户管理")
@RestController
@RequestMapping("/api/biz/user")
public class BizUserController extends BaseController<HsmwUserService, HsmwUser> {


	@Autowired
	HsmwRoleService hsmwRoleService;


	@Autowired
	HsmwUserService hsmwUserService;
	/**
	 * 获取用户的分页列表
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取用户的分页列表", notes = "获取用户的分页列表", produces = "application/json")
	@PostMapping("/getHsmwUserPage")
	public DataResponse getHsmwUserPage(@RequestBody @Valid BasePage<HsmwUser> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwUser hsmwUser = basePage.getRequestData();
		IPage<HsmwUser> data = hsmwUserService.getHsmwUserPage(page, hsmwUser);
		Map<String, String> roleMap = hsmwRoleService.list().stream().collect(Collectors.toMap(HsmwRole::getId, HsmwRole::getName));
		data.getRecords().stream().forEach(hsmwUser1 -> {
			if (StrUtil.isNotBlank(hsmwUser1.getRoleId())) {
				hsmwUser1.setRoleName(roleMap.get(hsmwUser1.getRoleId()));
			}
		});
		return new DataResponse(data);
	}

	/**
	 * 获取用户下拉列表
	 */
	@ApiOperation(value = "获取用户下拉列表", notes = "获取用户下拉列表", produces = "application/json")
	@PostMapping("/getHsmwUserSelect")
	public DataResponse getHsmwUserSelect() {
		List<HsmwUser> data = hsmwUserService.getHsmwUserSelect();
		return new DataResponse(data);
	}

	/**
	 * 根据ID查询数据
	 *
	 * @return
	 */
	@ApiOperation(value = "根据id查询数据", notes = "根据id查询数据", produces = "application/json")
	@PostMapping("/getHsmwUserById")
	public DataResponse getHsmwUserById(@RequestBody GetObjectByIdForm form) {
		HsmwUser hsmwUser = hsmwUserService.getHsmwUserById(form.getKey());
		return new DataResponse(hsmwUser);
	}

	/**
	 * 新增
	 * 修改  必须带ID
	 * 删除  逻辑删除 修改FLAG
	 * 启用/禁用 修改ENABLED
	 *
	 * @param hsmwUser
	 * @return
	 */
	@ApiOperation(value = "新增/修改/逻辑启用-禁用接口", notes = "修改：传id；删除：传id+flag；启用/禁用：传enabled", produces = "application/json")
	@PostMapping("/saveOrUpdateHsmwUser")
	public DataResponse saveOrUpdateHsmwUser(@RequestBody HsmwUser hsmwUser) {
		return hsmwUserService.saveOrUpdateHsmwUser(hsmwUser);
	}



	@ApiOperation(value = "设置角色", notes = "设置角色", produces = "application/json")
	@PostMapping("/setRoleOnUser")
	public DataResponse setRoleOnUser(String roleId,String userId){


		return new DataResponse();
	}



}
